package com.qizhu.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.qizhu.entity.Item;
import com.qizhu.entity.User;
import com.qizhu.service.IItemService;
import com.qizhu.util.FilterList;
import com.qizhu.util.JSONUtil;
import com.qizhu.util.PageResults;
import com.qizhu.util.Rule;
import com.qizhu.util.StringUtil;

/**
 * <p>
 * 口袋神婆商品（问题） 前端控制器
 * </p>
 *
 * @author 张权
 * @since 2017-04-18
 */
@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {
	@Autowired
	private IItemService itemService;

	@ResponseBody
	@RequestMapping("save")
	public Object save() {
		Item item = new Item();
		item.setName("十年大运");
		item.setIsAudit(1);
		item.setCatId("1");
		item.setPrice(1000);
		if (item.getItemId() == null) {
			return itemService.insert(item) ? renderSuccess("添加成功")
					: renderError("添加失败");
		} else {
			return itemService.updateById(item) ? renderSuccess("修改成功")
					: renderError("修改失败");
		}
	}
	/**
	 * 获取商品列表
	 * @return 返回json数据
	 */
	@ResponseBody
	@RequestMapping("getItems")
	public Object getItems(){
		EntityWrapper<Item> ew = new EntityWrapper<Item>();
//		ew.eq(StringUtil.camelToUnderline("catId"), "1");
		ew.setSqlSelect("item_id AS itemId,`name`");
		ew.where("cat_id = {0} AND name = {1}", "1","aaa");
//		ew.orNew(sqlOr, params)
		Page<Item> page = new Page<Item>();
		page.setCurrent(1);
		page.setSize(2);
		page.setOrderByField(StringUtil.camelToUnderline("name"));
		Map<String, Object> condition = new HashMap<String, Object>();
//		condition.put(StringUtil.camelToUnderline("catId"), "1");
		page.setCondition(condition );
		page.setAsc(false);
		itemService.selectPage(page, ew);
		return page;
	}
	/**
	 * 获取商品列表，封装jqgrid表格数据
	 * @param page page
	 * @param rows rows
	 * @param sidx sidx
	 * @param sord sord
	 * @param catId catId 
	 * @param searchField searchField
	 * @param searchOper searchOper
	 * @param searchString searchString
	 * @param filters filters
	 * @return 返回分页数据
	 */
	@ResponseBody
	@RequestMapping("/findItemJqgridJson")
	public PageResults<Item> findItemJqgridJson(
			@RequestParam(defaultValue = "1", value = "page") int page,
			@RequestParam(defaultValue = "10", value = "rows") int rows,
			@RequestParam(defaultValue = "name", value = "sidx") String sidx,
			@RequestParam(defaultValue = "desc", value = "sord") String sord,
			@RequestParam(value = "catId", required = false) String catId,
			// @RequestParam("isSearch") Boolean isSearch,
			@RequestParam(value = "searchField", required = false) String searchField,
			@RequestParam(value = "searchOper", required = false) String searchOper,
			@RequestParam(value = "searchString", required = false) String searchString,
			@RequestParam(value = "filters", required = false) String filters) {
		EntityWrapper<Item> ew = new EntityWrapper<Item>();
//		ew.eq(StringUtil.camelToUnderline("catId"), "1");
//		ew.orNew(sqlOr, params)
		Page<Item> pages = new Page<Item>();
		pages.setCurrent(page);
		pages.setSize(rows);
		
		
		
		
		
		if (StringUtils.isEmpty(sidx)) {
			pages.setOrderByField(StringUtil.camelToUnderline("name"));
		}else{
			pages.setOrderByField(StringUtil.camelToUnderline(sidx));
			if(StringUtils.equalsIgnoreCase(sord, "desc")){
				pages.setAsc(false);
			}else{
				pages.setAsc(true);
			}
		}
		//接收前台传来的搜索条件的字符串
		//解析为javabean
		// 添加搜索条件
		if (StringUtils.isNotEmpty(filters)) {
			FilterList filterList=(FilterList)JSONUtil.getObject(filters, FilterList.class);
				
			for (int i=0;i<filterList.getRules().size();i++) {
				Rule rule=filterList.getRules().get(i);
				//判断是否为模糊查询
    			if (StringUtils.equalsIgnoreCase(filterList.getGroupOp(), "AND")) {// 如果关系是并列
    				
    				ew.like(StringUtil.underlineToCamel(rule.getField()), rule.getData());
    				
    			}else if (StringUtils.equalsIgnoreCase(filterList.getGroupOp(),"OR")) {// 如果关系是或者
    				
    			}
			}
		}
		
		itemService.selectPage(pages, ew);
		
		PageResults<Item> pageResults = new PageResults<Item>();
		pageResults.setPage(page);
		pageResults.setRecords(pages.getTotal());
		pageResults.setResults(pages.getRecords());
		return pageResults;	
		
		

	}
	
	public static void main(String[] args) {
		System.out.println(String.format("我叫什么:%s","张权"));
		AtomicInteger paramNameSeq = new AtomicInteger(0);
		System.out.println(paramNameSeq.incrementAndGet());
		System.out.println(paramNameSeq.incrementAndGet());
		System.out.println(paramNameSeq.incrementAndGet());
		System.out.println(paramNameSeq.incrementAndGet());
		paramNameSeq.lazySet(10);
		System.out.println(paramNameSeq.incrementAndGet());
	}
}
